<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品分类" prop="cateId">
        <el-select
          v-model="queryParams.cateId"
          clearable
          placeholder="请选择商品分类"
          class="filter-item"
          filterable
          :filter-method="dataFilter"
        >
          <el-option
            v-for="item in optionsMetaShow"
            :disabled="item.disabled === 0"
            :value="item.value"
            :key="item.id"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="isShow">
        <el-select
          v-model="queryParams.isShow"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.goods_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:storeProduct:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:storeProduct:edit']"
          >修改</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="storeProductList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" prop="storeName" />
      <el-table-column label="商品图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="商品标签" align="center" prop="keyword" />
      <el-table-column label="商品售价" align="center" prop="price" />
      <el-table-column label="商品进价" align="center" prop="cost" />
      <el-table-column label="销量" align="center" prop="sales" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="状态" align="center" prop="isShow">
        <template slot-scope="scope">
          <el-tag effect="dark" type="success" v-if="scope.row.isShow == 1"
            >已上架</el-tag
          >
          <el-tag effect="dark" type="danger" v-if="scope.row.isShow == 0"
            >已下架</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:storeProduct:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:storeProduct:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listStoreProduct,
  delStoreProduct,
  listShowStoreProductCategory,
} from "@/api/business/storeProduct";

export default {
  name: "StoreProduct",
  dicts: ["goods_status", "hot_status", "rule_status"],
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API,
      //图片根路径
      imageURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      optionsMetaShow: [],
      storeProductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merId: null,
        image: null,
        sliderImage: null,
        storeName: null,
        storeInfo: null,
        keyword: null,
        barCode: null,
        cateId: null,
        price: null,
        vipPrice: null,
        otPrice: null,
        postage: null,
        unitName: null,
        sort: null,
        sales: null,
        stock: null,
        isShow: null,
        isHot: null,
        isBenefit: null,
        isBest: null,
        isNew: null,
        description: null,
        isPostage: null,
        giveIntegral: null,
        cost: null,
        isGood: null,
        ficti: null,
        browse: null,
        codePath: null,
        isSub: null,
        tempId: null,
        specType: null,
        isIntegral: null,
        integral: null,
        giveIntegralType: null,
      },
    };
  },
  created() {
    this.getList();
    this.getCategory();
  },
  methods: {
    dataFilter(val) {
      this.value = val;
      if (val) {
        this.optionsMetaShow = this.cateList.filter((item) => {
          if (
            !!~item.label.indexOf(val) ||
            !!~item.label.toUpperCase().indexOf(val.toUpperCase())
          ) {
            return true;
          }
        });
      } else {
        this.optionsMetaShow = this.cateList;
      }
    },

    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listStoreProduct(this.queryParams).then((response) => {
        this.storeProductList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取分类列表
    getCategory() {
      listShowStoreProductCategory().then((response) => {
        this.optionsMetaShow = response.data.cateList;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: "/goods/add" });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: "/goods/edit/" + row.id });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品编号为"' + ids + '"的数据项？')
        .then(function () {
          return delStoreProduct(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/storeProduct/export",
        {
          ...this.queryParams,
        },
        `storeProduct_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
